package com.metro.activation.common.utils;

import com.metro.core.user.BaseUserInfo;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;

/**
 * 用户信息帮助类
 * @author xk
 */
public class UserUtil {

    /**
     * 获取当前用户信息
     * @return 当前用户信息
     */
    public static BaseUserInfo getCurrUser() {
        Subject currentUser = SecurityUtils.getSubject();
        BaseUserInfo currUserInfo = null;
        if (currentUser != null && currentUser.getPrincipal() != null) {
            currUserInfo = (BaseUserInfo)currentUser.getPrincipal();
        }

        return currUserInfo;
    }

    /**
     * 获取当前用户id
     * @return 当前用户信息id
     */
    public static Long getCurrUserId() {
        Subject currentUser = SecurityUtils.getSubject();
        BaseUserInfo currUserInfo = null;
        if (currentUser != null && currentUser.getPrincipal() != null) {
            currUserInfo = (BaseUserInfo)currentUser.getPrincipal();
            return currUserInfo.getUserId();
        }

        return null;
    }

    /**
     * 获取当前用户名
     * @return 当前用户ming
     */
    public static String getCurrUserName() {
        Subject currentUser = SecurityUtils.getSubject();
        BaseUserInfo currUserInfo = null;
        if (currentUser != null) {
            currUserInfo = (BaseUserInfo)currentUser.getPrincipal();
            return currUserInfo.getUserName();
        }

        return null;
    }


    /**
     * 获取当前用户是否具有某个角色
     * @param roleCode 角色编码
     * @return
     */
    public static boolean hasRole(String roleCode) {
        Subject currentUser = SecurityUtils.getSubject();
        return currentUser.hasRole(roleCode);
    }
}
